package cn.zwx.activiti.example;

import cn.zwx.activiti.example.utils.SecurityUtil;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.task.model.builders.TaskPayloadBuilder;
import org.activiti.api.task.runtime.TaskRuntime;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskInfoQuery;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
class ActivitiExampleApplicationForGroup {

    @Autowired
    private SecurityUtil securityUtil;

    @Autowired
    private ProcessRuntime processRuntime;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskRuntime taskRuntime;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;


    /**
     * @description 部署工作流
     * @return void
     * @author zhangwenxue
     * @createTime 2021/5/6 21:28
     **/
    @Test
    void loadProcess() {
        securityUtil.logInAs("张三");
        // 部署
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("processes/group_task.bpmn")
                .addClasspathResource("processes/group_task.png").name("组任务流程").deploy();
        // 打印信息
        System.out.println(deployment.getName());
        System.out.println(deployment.getId());
    }


    /**
     * @description 启动流程实例
     * @return void
     * @author zhangwenxue
     * @createTime 2021/5/6 21:28
     **/
    @Test
    void startProcess() {
        securityUtil.logInAs("root");
        // 启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("group_task");
//        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("jiantingqi",variables);
        // 打印信息
        System.out.println("DeploymentId:"+processInstance.getDeploymentId());
        System.out.println("流程实例id:"+processInstance.getId());
        System.out.println("流程定义id:"+processInstance.getProcessDefinitionId());
    }

    /**
     * @description 组任务 处理
     * @return void
     * @author zhangwenxue
     * @createTime 2021/5/17 20:31
     **/
    @Test
    void groupTaskTest(){
        securityUtil.logInAs("张三");
        // 查询组任务
        TaskInfoQuery taskQuery = taskService.createTaskQuery();
        List<Task> groupList = taskQuery.taskCandidateUser("张三").orderByTaskCreateTime().asc().list();
//        List<Task> groupList = taskQuery.taskAssignee("张三").orderByTaskCreateTime().asc().list();
        for (Task task : groupList) {
            System.out.println("group_list start ===========================");
            System.out.println("流程实例id"+task.getProcessInstanceId());
            System.out.println("任务id:"+task.getId());
            System.out.println("部署id"+task.getProcessDefinitionId());
            System.out.println("任务负责人:"+task.getAssignee());
            System.out.println("任务名称"+task.getName());
            System.out.println("group_list end =============================");
//             抓起任务
            org.activiti.api.task.model.Task claim_task = taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(task.getId()).build());
//            taskService.claim();
            System.out.println("claim_task start ===========================");
            System.out.println("流程实例id"+claim_task.getProcessInstanceId());
            System.out.println("任务id:"+claim_task.getId());
            System.out.println("部署id"+claim_task.getProcessDefinitionId());
            System.out.println("任务负责人:"+claim_task.getAssignee());
            System.out.println("任务名称"+claim_task.getName());
            System.out.println("抓起事件"+claim_task.getClaimedDate());
            System.out.println("claim_task end =============================");

            // 取消抓起任务 TODO 方式一
//            org.activiti.api.task.model.Task release = taskRuntime.release(TaskPayloadBuilder.release().withTaskId(task.getId()).build());
            // TODO 方式二 不会返回取消任务
//            taskService.unclaim(task.getId());
            // TODO 方式三
//            taskService.setAssignee(task.getId(),null);

            // 任务的交接
            // 完成任务
//            taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(claim_task.getId()).build());
//            taskService.complete();

//
//            System.out.println("release task===========================");
//            System.out.println("流程实例id"+release.getProcessInstanceId());
//            System.out.println("任务id:"+release.getId());
//            System.out.println("部署id"+release.getProcessDefinitionId());
//            System.out.println("任务负责人:"+release.getAssignee());
//            System.out.println("任务名称"+release.getName());
//            System.out.println("release task=============================");
        }
    }

    /**
     * @description 任务交接
     * @author zhangwenxue
     * @createTime 2021/5/17 21:45
     **/
    @Test
    void groupTaskTransfer(){
        String transfer_name = "李四";
        TaskInfoQuery taskQuery = taskService.createTaskQuery();
        List<Task> taskList = taskQuery.taskAssignee("张三").orderByTaskCreateTime().asc().list();
        for (Task task : taskList) {
            taskService.setAssignee(task.getId(),transfer_name);
        }

      }

}
